// Bog.cpp : Implementation of CBog
#include "stdafx.h"
#include "EBASdll.h"
#include "Bog.h"
#include "importado.h"

/////////////////////////////////////////////////////////////////////////////
// CBog

STDMETHODIMP CBog::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IBog
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP CBog::get_Titel(BSTR *pVal)
{
	*pVal = m_titel.copy();

	return S_OK;
}

STDMETHODIMP CBog::put_Titel(BSTR newVal)
{
	m_titel = newVal;

	return S_OK;
}

STDMETHODIMP CBog::get_Forfatter(BSTR *pVal)
{
	*pVal = m_forfatter.copy();

	return S_OK;
}

STDMETHODIMP CBog::put_Forfatter(BSTR newVal)
{
	m_forfatter = newVal;

	return S_OK;
}

STDMETHODIMP CBog::get_Forlag(BSTR *pVal)
{
	*pVal = m_forlag.copy();

	return S_OK;
}

STDMETHODIMP CBog::put_Forlag(BSTR newVal)
{
	m_forlag = newVal;

	return S_OK;
}

STDMETHODIMP CBog::get_ISBN(BSTR *pVal)
{
	*pVal = m_isbn.copy();

	return S_OK;
}

STDMETHODIMP CBog::put_ISBN(BSTR newVal)
{
	m_isbn = newVal;

	return S_OK;
}

STDMETHODIMP CBog::get_GUID(BSTR *pVal)
{
	*pVal = m_guid.copy();

	return S_OK;
}

STDMETHODIMP CBog::SetInfo()
{
	_CommandPtr command;
	_RecordsetPtr recordset;

	const char sql[255] = "EBAS_SP_GEM_BOG";

	try {
		command.CreateInstance(__uuidof(Command));		
		command->ActiveConnection = CDatabase::GetConnection();
		command->CommandText = sql;
		command->CommandType = adCmdStoredProc;
		command->Parameters->Append( command->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		command->Parameters->Append( command->CreateParameter("titel", adVarChar, adParamInput, 255, m_titel) );
		command->Parameters->Append( command->CreateParameter("forfatter", adVarChar, adParamInput, 255, m_forfatter) );
		command->Parameters->Append( command->CreateParameter("forlag", adVarChar, adParamInput, 255, m_forlag) );
		command->Parameters->Append( command->CreateParameter("isbn", adVarChar, adParamInput, 255, m_isbn) );		

		recordset = command->Execute(NULL, NULL, adCmdStoredProc);
		if(recordset->ADO_EOF)
			return E_ACCESSDENIED;

		m_guid = (bstr_t)recordset->Fields->GetItem("GUID")->Value;
		
		if (recordset)
			recordset->Close();
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}
	return S_OK;
}

STDMETHODIMP CBog::Slet()
{
	_CommandPtr command;
	_RecordsetPtr recordset;

	const char sql[255] = "EBAS_SP_SLET_BOG";

	try {
		command.CreateInstance(__uuidof(Command));		
		command->ActiveConnection = CDatabase::GetConnection();
		command->CommandText = sql;
		command->CommandType = adCmdStoredProc;
		command->Parameters->Append( command->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		command->Parameters->Append( command->CreateParameter("guid", adVarChar, adParamInput, 255, m_guid) );
		
		recordset = command->Execute(NULL, NULL, adCmdStoredProc);
		if(recordset->ADO_EOF)
			return E_ACCESSDENIED;

		m_guid = bstr_t("");
		m_forfatter = bstr_t("");
		m_forlag = bstr_t("");
		m_isbn = bstr_t("");
		m_titel = bstr_t("");
		
		if (recordset)
			recordset->Close();
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}
	return S_OK;
}
